// pages/GoodsClassify/GoodsClassify.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    classify_data: [],
    store_id: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.store_id) {
      this.setData({
        store_id: options.store_id
      });
    }
    this.getJson();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {
    wx.reLaunch({
      url:'/packageC/o2o/storeManage/storeManage?store_id='+this.data.store_id,
    })
  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  getJson() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods-category.get-category-by-store-id');
    let json = {
      store_id: parseInt(that.data.store_id)
    };
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            classify_data: []
          });
          for (let i = 0; i < res.data.list.length; i++) {
            res.data.list[i].btn_active = false;
            res.data.list[i].level_active = false;
            for (var k = 0; k < res.data.list[i].childrens.length; k++) {
              res.data.list[i].childrens[k].btn_active = false;
              res.data.list[i].childrens[k].level_active = false;
            }
          }
          that.setData({
            classify_data: res.data.list
          });
          that.emptyMore();
        } else {
          wx.showToast({
            icon: 'none',
            title: '暂无商品分类,请添加分类',
            duration: 1500
          });
        }

      },
      fail: function(res) {}
    });
  },
  //清空
  emptyMore() {
    for (var i = 0; i < this.data.classify_data.length; i++) {
      this.setData({
        ['classify_data[' + i + '].btn_active']: false,
        ['classify_data[' + i + '].level_active']: false
      });
      for (var k = 0; k < this.data.classify_data[i].childrens.length; k++) {
        this.setData({
          ['classify_data[' + i + '].childrens[' + k + '].btn_active']: false,
          ['classify_data[' + i + '].childrens[' + k + '].level_active']: false
        });
      }
    }
  },
  //展开
  oneLevel(e) {
    let index = e.currentTarget.dataset.index;
    let level = e.currentTarget.dataset.num;
    if (level == 1) {
      if (this.data.classify_data[index].level_active == true) {
        this.emptyMore();
      } else {
        if (this.data.classify_data[index].childrens.length != 0) {
          this.emptyMore();
          this.setData({
            ['classify_data[' + index + '].level_active']: true
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: '此类暂无数据',
            duration: 1500
          });
        }
      }
    } else if (level == 2) {
      if (this.data.classify_data[index].childrens[index2].level_active == true) {
        this.emptyMore();
        this.setData({
          ['classify_data[' + index + '].level_active']: true
        });
      } else {
        if (this.data.classify_data[index].childrens[index2].thirds.length != 0) {
          this.emptyMore();
          this.setData({
            ['classify_data[' + index + '].level_active']: true,
            ['classify_data[' + index + '].childrens[' + index2 + '].level_active']: true
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: '此类暂无数据',
            duration: 1500
          });
        }
      }
    }
  },
  //更多
  levelMore(e) {
    let level = e.currentTarget.dataset.num;
    let index = e.currentTarget.dataset.index;
    let index2 = e.currentTarget.dataset.indexc;
    if (level == 1) {
      if (this.data.classify_data[index].btn_active == true) {
        this.emptyMore();
      } else {
        this.emptyMore();
        this.setData({
          ['classify_data[' + index + '].btn_active']: true
        });
      }
    } else if (level == 2) {
      if (this.data.classify_data[index].childrens[index2].btn_active == true) {
        this.emptyMore();
        this.setData({
          ['classify_data[' + index + '].level_active']: true
        });
      } else {
        this.emptyMore();
        this.setData({
          ['classify_data[' + index + '].level_active']: true,
          ['classify_data[' + index + '].childrens[' + index2 + '].btn_active']: true
        });
      }
    } else if (level == 3) {
      if (this.data.classify_data[index].childrens[index2].thirds[index3].btn_active == true) {
        this.emptyMore();
        this.setData({
          ['classify_data[' + index + '].level_active']: true,
          ['classify_data[' + index + '].childrens[' + index2 + '].level_active']: true
        });
      } else {
        this.emptyMore();
        this.setData({
          ['classify_data[' + index + '].level_active']: true,
          ['classify_data[' + index + '].childrens[' + index2 + '].level_active']: true,
          ['classify_data[' + index + '].childrens[' + index2 + '].thirds[' + index3 + '].btn_active']: true
        });
      }
    }

  },
  classifyRoute(e) {
    let params = e.currentTarget.dataset.num;
    let level = e.currentTarget.dataset.numc;
    let index = e.currentTarget.dataset.index;
    let index2 = e.currentTarget.dataset.indexc;
    if (params == 1) {
      if (level == 2) {
        wx.navigateTo({
          url: '/packageC/o2o/AddClassify/AddClassify?store_id=' + this.data.store_id + '&classify_id=' + this.data.classify_data[
            index].id + '&level=' + level
        });
      } else if (level == 3) {
        wx.navigateTo({
          url: '/packageC/o2o/AddClassify/AddClassify?store_id=' + this.data.store_id + '&classify_id=' + this.classify_data[
            index].childrens[index2].id + '&level=' + level
        });
      }
    } else if (params == 2) {
      if (level == 1) {
        wx.navigateTo({
          url: '/packageC/o2o/FirstEdit/FirstEdit?store_id=' + this.data.store_id + '&classify_id=' + this.data.classify_data[
            index].id + '&level=' + level
        });
      } else if (level == 2) {
        wx.navigateTo({
          url: '/packageC/o2o/FirstEdit/FirstEdit?store_id=' + this.data.store_id + '&classify_id=' + this.data.classify_data[
            index].childrens[index2].id + '&level=' + level
        });
      } else if (level == 3) {
        wx.navigateTo({
          url: '/packageC/o2o/FirstEdit/FirstEdit?store_id=' + this.data.store_id + '&classify_id=' + this.data.classify_data[
            index].childrens[index2].thirds[index3].id + '&level=' + level
        });
      }
    }
  },
  //删除
  deleteClassify(e) {
    let params = e.currentTarget.dataset.num;
    let index = e.currentTarget.dataset.index;
    let index2 = e.currentTarget.dataset.indexc;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '此操作将永久删除该分类, 是否继续?',
      success(res) {
        if (res.confirm) {
          let deleteId = '';
          if (params == 1) {
            console.log(params);
            deleteId = that.data.classify_data[index].id;
            if (that.data.classify_data[index].childrens.length != 0) {
              wx.showToast({
                icon: 'none',
                title: '先清空二级分类',
                duration: 1500
              });
              return;
            }
          } else if (params == 2) {
            deleteId = that.data.classify_data[index].childrens[index2].id;

          } else if (params == 3) {
            deleteId = that.data.classify_data[index].childrens[index2].thirds[index3].id;
          }
          let json = {
            id: deleteId
          };
          let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods-category.del-category');
          app._postNetWork({
            url: urlStr,
            showToastIn: false,
            data: json,
            success: function(resdata) {
              var res = resdata.data;
              if (res.result == 1) {
                let classify_data = that.data.classify_data;
                if (params == 1) {
                  classify_data.splice(index, 1);
                } else if (params == 2) {
                  classify_data[index].childrens.splice(index2, 1);
                } else if (params == 3) {
                  classify_data[index].childrens[index2].thirds.splice(index3, 1);
                }
                that.setData({
                  classify_data: classify_data
                });
                wx.showToast({
                  icon: 'none',
                  title: '删除成功',
                  duration: 1500
                });
              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }

            },
            fail: function(res) {}
          });
        } else if (res.cancel) {
          wx.showToast({
            icon: 'none',
            title: '已取消删除',
            duration: 1500
          });
        }
      }
    });
  },
  addClassify() {
    wx.navigateTo({
      url: '/packageC/o2o/AddClassify/AddClassify?store_id=' + this.data.store_id + '&classify_id=add' + '&level=1'
    });
  },
});
